import { Controller, Get, Post, Body, Patch, Param, Delete, Res, HttpStatus, HttpException } from '@nestjs/common';
import { LocalfileService } from './localfile.service';
import { CreateLocalfileDto } from './dto/create-localfile.dto';
import { UpdateLocalfileDto } from './dto/update-localfile.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { UseInterceptors } from '@nestjs/common';
import { UploadedFile } from '@nestjs/common';
import * as path from 'path';

@Controller('localfile')
export class LocalfileController {
  constructor(private readonly localfileService: LocalfileService) { }

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile() file) {
    if (!file) {
      return {
        code: 400,
        'Content-Type': 'application/json',
        links:'URL_ADDRESS',
        data: {
          success: false,
          message: 'No file uploaded'
        },
      };
    }
    return { success: true, filePath: file.path };
  }
  // @Get()
  // async getFile(): Promise<string[]> {
  //   return this.localfileService.getFile();

  // }
  @Get(':folder')
  async getFiles(@Param('folder') folder: string): Promise<string[]> {
    console.log(process.env.OSS_test);


    return this.localfileService.getFiles(folder);
  }

  // 

  @Get('view/:folder/:filename')
  async viewFile(@Param('folder') folder: string, @Param('filename') filename: string, @Res() res: any) {
    const filePath = this.localfileService.getFilePath(folder, filename);
    console.log(filePath);

    try {
      const mimeType = this.getMimeType(filePath);
      res.set({
        'Content-Type': mimeType,
        'Content-Disposition': 'inline; filename="' + filename + '"',
      });
      res.sendFile(filePath);
    } catch (error) {
      console.log(error);

      throw new HttpException('File not found', HttpStatus.NOT_FOUND);

    }
  }
  private getMimeType(filePath: string): string {
    const ext = path.extname(filePath).toLowerCase();
    switch (ext) {
      case '.html':
        return 'text/html';
      case '.js':
        return 'text/javascript';
      case '.css':
        return 'text/css';
      case '.json':
        return 'application/json';
      case '.png':
        return 'image/png';
      case '.jpg':
      case '.jpeg':
        return 'image/jpeg';
      case '.gif':
        return 'image/gif';
      case '.svg':
        return 'image/svg+xml';
      default:
        return 'application/octet-stream';
    }
  }



  @Post()
  create(@Body() createLocalfileDto: CreateLocalfileDto) {
    return this.localfileService.create(createLocalfileDto);
  }

  @Get()
  findAll() {
    return this.localfileService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.localfileService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateLocalfileDto: UpdateLocalfileDto) {
    return this.localfileService.update(+id, updateLocalfileDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.localfileService.remove(+id);
  }
}
